$(document).ready(function(){
    var $url = $('body').attr('data-siteurl');
//My Account--------------------------------------------------------------------
     $('.btn-edit-acc-profile').click(function(){
        $('.view-account-profile').fadeOut('fast', function(){
            $(this).addClass('hide');
        });
        $('.edit-account-profile').fadeIn('fast', function(){
            $(this).removeClass('hide');
        });
        $(this).addClass('hide');
        $('.btn-change-pass').addClass('hide');
        $('.btn-update-acc-profile').removeClass('hide');
        $('.btn-cancel-acc-profile').removeClass('hide');
    });
    $('.btn-cancel-acc-profile').click(function(){
        $('.view-account-profile').fadeIn('fast', function(){
                $(this).removeClass('hide');
            });
        $('.edit-account-profile').fadeOut('fast', function(){
            $(this).addClass('hide');
        });        
        $('.btn-change-pass').removeClass('hide');
        $('.btn-edit-acc-profile').removeClass('hide');
    });
    $('.btn-change-pass').click(function(){
        $('.view-account-profile').fadeOut('fast', function(){
            $(this).addClass('hide');
        });
        $('.edit-account-profile').fadeIn('fast', function(){
            $(this).addClass('hide');
        });
        $('.change-password').fadeIn('fast', function(){
            $(this).removeClass('hide');
        });
        $(this).addClass('hide');
        $('.btn-edit-acc-profile').addClass('hide');
        $('.btn-update-password').removeClass('hide');
        $('.btn-cancel-change-pass').removeClass('hide');
    });
    $('.btn-cancel-change-pass').click(function(){
        $('.view-account-profile').fadeOut('fast', function(){
            $(this).removeClass('hide');
        });
        $('.edit-account-profile').fadeIn('fast', function(){
            $(this).addClass('hide');
        });
        $('.change-password').fadeIn('fast', function(){
            $(this).addClass('hide');
        });  
        $('.btn-change-pass').removeClass('hide');
        $('.btn-edit-acc-profile').removeClass('hide');
    });
//Manage Resource---------------------------------------------------------------
    //Files---------------------------------------------------------------------
    $('#upload-file-popup .btn-upload').click(function(){
        if($('#filename').val()!=='' && $('#userfile').val()!==''){
            var $site_url = $('body').attr('data-siteurl');
            $.ajaxFileUpload({
                url: $site_url + 'file/upload',
                secureuri: false,
                fileElementId: 'userfile',
                dataType: 'json',
                data: {'filename':$('#filename').val(),'type':$('.select-file-type option:selected').val()},
                success: function(data, status) {
                    if(data.status !== 'error'){
//                        var type = '';
//                        if(data.file_data.file_type=='0'){
//                            type = 'Document';
//                        }else{
//                            type = 'Report';
//                        }
//                        $('.files-list tbody').prepend('<tr>'+
//                                '<td><input type="checkbox" class="item-file-checkbox" value="'+data.file_data.file_id+'"/></td>'+
//                                '<td>'+data.file_data.file_name+'</td>'+
//                                '<td>'+type+'</td>'+
//                                '<td>'+data.file_data.date_uploaded+'</td>'+
//                                '<td><a href="'+$site_url+data.file_data.file_path+'" class="btn btn-primary btn-xs" role="button" target="_blank" title="Download"><span class="glyphicon glyphicon-download"></span></a></td>'+
//                            '</tr>');
                        $('#files-table').dataTable( {
                                "bProcessing": true,
                                "sAjaxSource": $url + ''
                        } );
                        $('#upload-file-popup').modal('hide');
                        $('.alert-success').remove();
                        $('.btn-delete-files').parent().parent().parent().after('<div class="alert alert-success">'+data.msg+'</div>');
                        success_msg();
                    }else{
                        $('#upload-file-popup .modal-body').prepend('<div class="alert alert-danger">'+data.msg+'</div>');
                    }
                },
                error: function(){
                    alert('error');
                }
            });
            return false;
        }else{
            if(!$('#upload-file-popup .modal-body span').hasClass('required-error')){
                $('#upload-file-popup .modal-body .required').after('<span class="required-error">This field is required.</span>');
            }
        }
    });
    $('#upload-file-popup').on('hidden.bs.modal', function(){
        $('#upload-file-popup .alert-danger').remove();
        $('#upload-file-popup .required-error').remove();
        $('#upload-file-popup #filename').val('');
        $('#upload-file-popup #userfile').val('');
    });
    $('.files-list .check-all-files').click(function(){
        if($(this).is(':checked')) {
            if($('.files-list tr td input[class=item-file-checkbox]').hasClass('item-file-checkbox')){
                $('.btn-delete-files').removeClass('hide-element');
                $('.files-list tr td input[class=item-file-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-files').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-files').addClass('hide-element');
            $('.files-list tr td input[class=item-file-checkbox]').prop('checked', false);
        }
    });
    $('.files-list').on('click', '.item-file-checkbox', function(){
        var $checked_item = $('.files-list .item-file-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-files').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-files').removeClass('hide-element');
            }else{
                $('.btn-delete-files').addClass('hide-element');
                $('.files-list .check-all-files').prop('checked', false);
            }
        }
    });
    $('#delete-file-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var file_id = new Array();
        $('.files-list .item-file-checkbox:checked').each(function(){
            file_id.push($(this).val());
        });
        var file = {'file_id':file_id};
        $.ajax({
            url: $site_url + 'files/delete',
            type: 'post',
            data: file,
            success: function (){
                $('#delete-file-popup').modal('hide');
                $('.files-list .check-all-files').prop('checked', false);
                $('.files-list .item-file-checkbox:checked').parent().parent().remove();
                $('.btn-delete-files').addClass('hide-element');
                $('.alert-success').remove();
                $('.btn-delete-files').parent().parent().parent().after('<div class="alert alert-success">File/s successfully deleted.</div>');
                success_msg();
            },
             error: function(){
                 alert('error');
             }
         });
    });
    $('.filter-files').change(function(){
        var $site_url = $('body').attr('data-siteurl');
        var filter = $('.filter-files option:selected').val();
        $('.files-list tbody').empty();
        $.ajax({
            url: $site_url + 'files/filter/' + filter,
            type: 'post',
            dataType: 'json',
            success: function (data){
                $.each(data, function(key, value){
                    var type = '';
                    if(value.file_type==0){
                        type = 'Document';
                    }else{
                        type = 'Report';
                    }
                   $('.files-list tbody').append('<tr>'+  
                           '<td><input type="checkbox" class="item-file-checkbox" value="'+value.file_id+'"/></td>'+
                            '<td>'+value.file_name+'</td>'+  
                            '<td>'+type+'</td>'+
                            '<td>'+value.date_uploaded+'</td>'+
                            '<td><a href="'+$site_url+value.file_path+'" class="btn btn-primary btn-xs" role="button" target="_blank" title="Download"><span class="glyphicon glyphicon-download"></span></a></td>'+
                            '</tr>'); 
                });
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //Venues--------------------------------------------------------------------
    $('.venues-nav a[data-toggle="tab"]').on('shown.bs.tab', function(e){
        var $site_url = $('body').attr('data-siteurl');
        $('.venues-list tbody').empty();
        $.ajax({
            url: $site_url + 'venues/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                $.each(data, function(key, value){
                    $('.venues-list tbody').append('<tr>'+
                            '<td><input type="checkbox" class="item-venue-checkbox" value="'+
                            value.venue_id+'" /></td>'+
                            '<td class="text-venue-name">'+value.venue_name+'</td>'+
                            '<td class="text-venue-address">'+value.venue_address+'</td>'+
                            '<td><a class="btn btn-primary btn-edit-venues btn-xs" data-toggle="modal"'+
                            'href="#edit-venue-popup" data-venueid="'+value.venue_id+'" title="Edit Venue"><span class="glyphicon glyphicon-edit"></span>'+
                            '</a></td>'+
                            '</tr>');
                });
            },
            error: function(){
                alert('error');
            }
        });
    });
    $('#new-venue-popup .btn-save').click(function(){
        if($('#n-venue-name').val()!=='' && $('#n-venue-address').val()!==''){
            var $site_url = $('body').attr('data-siteurl');
            var venue = {'venue_name':$('#n-venue-name').val(),
                    'venue_address':$('#n-venue-address').val(),
                    'action':$('#n-venue-action').val(),
                };
            save_venue($site_url, venue, $('#new-venue-popup').attr('id'));
        }else{
            if(!$('#new-venue-popup .modal-body span').hasClass('required-error')){
                $('#new-venue-popup .modal-body .required').after('<span class="required-error">This field is required.</span>');
            }
        }
    });
    $('#new-venue-popup').on('hidden.bs.modal', function(){
        $('#new-venue-popup .alert-danger').remove();
        $('#new-venue-popup .required-error').remove();
        $('#new-venue-popup #n-venue-name').val('');
        $('#new-venue-popup #n-venue-address').val('');
    });
    $('.venues-list').on('click', 'tbody tr td .btn-edit-venues', function(){
        $('#edit-venue-popup #e-venue-id').val($(this).attr('data-venueid'));
        $(this).addClass('updated-venue');
    });
    $('#edit-venue-popup').on('show.bs.modal', function(){
        var $site_url = $('body').attr('data-siteurl');
        var $venue_id = $('#edit-venue-popup #e-venue-id').val();
        get_venue($site_url, $venue_id);
    });
    $('#edit-venue-popup .btn-update').click(function(){
        if($('#e-venue-name').val()!=='' && $('#e-venue-address').val()!==''){
            var $site_url = $('body').attr('data-siteurl');
            var venue = {'venue_name':$('#e-venue-name').val(),
                    'venue_address':$('#e-venue-address').val(),
                    'action':$('#e-venue-action').val(),
                    'venue_id':$('#e-venue-id').val(),
                };
            save_venue($site_url, venue, $('#edit-venue-popup').attr('id'));
        }else{
            if(!$('#new-venue-popup .modal-body span').hasClass('required-error')){
                $('#new-venue-popup .modal-body .required').after('<span class="required-error">This field is required.</span>');
            }
        }
    });
    $('#edit-venue-popup').on('hidden.bs.modal', function(){
        $('#edit-venue-popup .alert-danger').remove();
        $('#edit-venue-popup .required-error').remove();
        $('#edit-venue-popup #e-venue-id').val('');
        $('#edit-venue-popup #e-venue-name').val('');
        $('#edit-venue-popup #e-venue-address').val('');
        $('.venues-list').find('.updated-venue').removeClass('updated-venue');
    }); 
    $('.venues-list .check-all-venues').click(function(){
        if($(this).is(':checked')) {
            if($('.venues-list tr td input[class=item-venue-checkbox]').hasClass('item-venue-checkbox')){
                $('.btn-delete-venues').removeClass('hide-element');
                $('.venues-list tr td input[class=item-venue-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-venues').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-venues').addClass('hide-element');
            $('.venues-list tr td input[class=item-venue-checkbox]').prop('checked', false);
        }
    });
    $('.venues-list').on('click', '.item-venue-checkbox', function(){
        var $checked_item = $('.venues-list .item-venue-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-venues').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-venues').removeClass('hide-element');
            }else{
                $('.btn-delete-venues').addClass('hide-element');
                $('.venues-list .check-all-venues').prop('checked', false);
            }
        }
    });
    $('#delete-venue-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var venue_id = new Array();
        $('.venues-list .item-venue-checkbox:checked').each(function(){
            venue_id.push($(this).val());
        });
        var venue = {'venue_id':venue_id};
        $.ajax({
            url: $site_url + 'venues/delete',
            type: 'post',
            data: venue,
            success: function (){
                $('#delete-venue-popup').modal('hide');
                $('.alert-success').remove();
                $('.btn-delete-venues').before('<div class="alert alert-success">Venue/s successfully deleted.</div>');
                success_msg();
                $('.venues-list .check-all-venues').prop('checked', false);
                $('.venues-list .item-venue-checkbox:checked').parent().parent().remove();
                $('.btn-delete-venues').addClass('hide-element');
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //Event Types---------------------------------------------------------------
    $('.event-types-nav a[data-toggle="tab"]').on('shown.bs.tab', function(e){
        var $site_url = $('body').attr('data-siteurl');
        $('.event-types-list tbody').empty();
        $.ajax({
            url: $site_url + 'event-types/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                $.each(data, function(key, value){
                    $('.event-types-list tbody').append('<tr>'+
                            '<td><input type="checkbox" class="item-event-type-checkbox" value="'+
                            value.type_id+'" /></td>'+
                            '<td>'+value.type_name+'</td>'+
                            '</tr>');
                });
            },
            error: function(){
                alert('error');
            }
        });
    });
    $('.btn-new-event-type').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        if($('#event-type-name').val()!==''){
           $.ajax({
               url: $site_url + 'event-type/save',
               type: 'post',
               data: {'event_type_name':$('#event-type-name').val()},
               dataType: 'json',
               success: function(data){
                   if(data.status==='success'){
                        $('.event-types-list tbody').prepend('<tr>'+
                            '<td><input type="checkbox" class="item-event-type-checkbox" value="'+
                            data.type.type_id+'" /></td>'+
                            '<td>'+data.type.type_name+'</td>'+
                            '</tr>');
                        $('#event-type-name').val('');
                        $('.alert-success').remove();
                        $('.btn-delete-event-types').before('<div class="alert alert-success">Event Type <b>'+data.type.type_name+'</b> successfully saved.</div>');
                        success_msg();
                   }else{
                        $('.alert-success').remove();
                        $('.btn-delete-event-types').before('<div class="alert alert-success">Event type successfully saved.</div>');
                        success_msg();
                   }
               },
               error: function(){
                   alert('error');
               }
           });
       }else{
           if(!$('.new-event-type-form p').hasClass('required-error')){
               $('.new-event-type-form .required').parent().after('<p class="required-error new-event-type-error">This field is required.</p>');
           }
       }
    });
    $('.event-types-list .check-all-event-types').click(function(){
        if($(this).is(':checked')) {
            if($('.event-types-list tr td input[class=item-event-type-checkbox]').hasClass('item-event-type-checkbox')){
                $('.btn-delete-event-types').removeClass('hide-element');
                $('.event-types-list tr td input[class=item-event-type-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-event-types').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-event-types').addClass('hide-element');
            $('.event-types-list tr td input[class=item-event-type-checkbox]').prop('checked', false);
        }
    });
    $('.event-types-list').on('click', '.item-event-type-checkbox', function(){
        var $checked_item = $('.event-types-list .item-event-type-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-event-types').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-event-types').removeClass('hide-element');
            }else{
                $('.btn-delete-event-types').addClass('hide-element');
                $('.event-types-list .check-all-event-types').prop('checked', false);
            }
        }
    });
    $('#delete-event-type-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var type_id = new Array();
        $('.event-types-list .item-event-type-checkbox:checked').each(function(){
            type_id.push($(this).val());
        });
        var type = {'type_id':type_id};
        $.ajax({
            url: $site_url + 'event-types/delete',
            type: 'post',
            data: type,
            success: function (){
                $('#delete-event-type-popup').modal('hide');
                $('.alert-success').remove();
                $('.btn-delete-event-types').before('<div class="alert alert-success">Event Type/s successfully deleted.</div>');
                success_msg();
                $('.event-types-list .check-all-event-types').prop('checked', false);
                $('.event-types-list .item-event-type-checkbox:checked').parent().parent().remove();
                $('.btn-delete-event-types').addClass('hide-element');
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //General Checklist---------------------------------------------------------
    $('.general-checklist-nav a[data-toggle="tab"]').on('shown.bs.tab', function(e){
        var $site_url = $('body').attr('data-siteurl');
        $('#general-checklist #accordion').empty();
        $.ajax({
            url: $site_url + 'general-checklist/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                $.each(data, function(key, value){
                    $('#general-checklist #accordion').prepend('<div class="panel panel-default">'+
                            '<div class="panel-heading"><h4 class="panel-title">'+
                            '<a data-toggle="collapse" data-parent="#accordion" href="#'+value.category_name.replace(/ /g, '-').toLowerCase()+'">'+
                            value.category_name+'</a><a href="#delete-cat-popup" data-catid="'+value.category_id+
                            '" class="btn btn-primary btn-delete-cat btn-xs pull-right"'+ 
                            'data-toggle="modal" title="Delete Category"><span class="glyphicon glyphicon-remove"></span></a>'+
                            '</h4></div><div id="'+value.category_name.replace(/ /g, '-').toLowerCase()+'" class="panel-collapse collapse"><div class="panel-body">'+
                            '<div class="list-group"><div class="form-inline new-sub-category-form"><div class="input-group"><input type="text" data-catid="'+value.category_id+
                            '" class="form-control g-c-sub-cat-name" name="sub_cat_name" placeholder="New Sub Category...">'+
                            '<span class="input-group-btn"><button class="btn btn-default btn-new-g-c-sub-cat" type="button"><span class="glyphicon glyphicon-plus"></span></button>'+
                            '</span></div></div><div class="sub-cat-list"></div></div></div></div></div>');
                });
            },
            error: function(){
                alert('error');
            }
        });
    });
    $('#general-checklist').on('click', 'a[data-toggle="collapse"]', function(){
        var $site_url = $('body').attr('data-siteurl');
        var $cat_id = $(this).siblings().attr('data-catid');
        var $target = $(this).parent().parent().siblings().children().children().children('.sub-cat-list');
        if($(this).parent().parent().siblings().hasClass('collapse')){
            if($target.children().length === 0){
                $.ajax({
                    url: $site_url + 'general-checklist/sub-cat/' + $cat_id,
                    type: 'post',
                    dataType: 'json',
                    success: function(sub_cat) {
                        if(sub_cat !== false) {
                            $.each(sub_cat, function(key, value){
                                $target.append('<a href="#delete-sub-cat-popup" class="list-group-item sub-cat-item" data-toggle="modal" data-catid="'+value.category_id+
                                        '" data-subcatid="'+value.checklist_id+'">'+value.checklist_description+
                                        '<span class="glyphicon glyphicon-remove pull-right"></span></a>'); 
                            });
                        }else{
                            if($target.find('.empty-msg').length===0){
                                $target.append('<a href="#" class="list-group-item empty-msg">Oops! No sub category found.</a>');
                            }
                        }
                    },
                    error: function(){
                        alert('error');
                    }
                });
            }
        }
    });
    $('.btn-new-g-c-category').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        if($('#g-c-category-name').val() !== '') {
            $.ajax({
                url: $site_url + 'general-checklist/save-new-cat',
                type: 'post',
                dataType: 'json',
                data: {'category':$('#g-c-category-name').val()},
                success: function(data){
                    if(data.status === 'success'){
                        $('#general-checklist #accordion').prepend('<div class="panel panel-default">'+
                            '<div class="panel-heading"><h4 class="panel-title">'+
                            '<a data-toggle="collapse" data-parent="#accordion" href="#'+data.cat.category_name.replace(/ /g, '-').toLowerCase()+'">'+
                            data.cat.category_name+'</a><a href="#delete-cat-popup" data-catid="'+data.cat.category_id+
                            '" class="btn btn-primary btn-delete-cat btn-xs pull-right"'+ 
                            'data-toggle="modal" title="Delete Category"><span class="glyphicon glyphicon-remove"></span></a>'+
                            '</h4></div><div id="'+data.cat.category_name.replace(/ /g, '-').toLowerCase()+'" data-catid="'+data.cat.category_id+'" class="panel-collapse collapse"><div class="panel-body">'+
                            '<div class="list-group"><div class="form-inline new-sub-category-form"><div class="input-group"><input type="text" class="form-control g-c-sub-cat-name" name="sub_cat_name" placeholder="New Sub Category...">'+
                            '<span class="input-group-btn"><button class="btn btn-default btn-new-g-c-sub-cat" type="button"><span class="glyphicon glyphicon-plus"></span></button>'+
                            '</span></div></div><div class="sub-cat-list"></div></div></div></div></div>');
                        $('#g-c-category-name').val('');
                        $('.new-category-form').find('.required-error').remove();
                        $('.alert-success').remove();
                        $('.btn-delete-event-types').before('<div class="alert alert-success">Event Type/s successfully deleted.</div>');
                        success_msg();
                    }else{
                        alert('error');
                    }
                },
                error: function(){
                    alert('error');
                }
            });
        }else{
            if(!$('.new-category-form p').hasClass('required-error')){
                $('.new-category-form').append('<p class="required-error">This field is required.</p>');
            }
        }
    });
    $('#general-checklist #accordion').on('click', '.btn-delete-cat', function(){
        var $catId = $(this).attr('data-catid');
        $('#delete-cat-popup .btn-yes').attr('data-catid', $catId);
        $(this).addClass('cat-to-be-deleted');
    });
    $('#delete-cat-popup .btn-yes').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $catId = $(this).attr('data-catid');
        $.ajax({
            url: $siteUrl + 'general-checklist/delete-cat/' + $catId,
            type: 'post',
            success: function(){
                $('#general-checklist #accordion').find('.cat-to-be-deleted').parent().parent().parent().remove();
                $('#delete-cat-popup').modal('hide');
            },
            error: function (){
               alert('error');
            }
        });
    });
    $('#delete-cat-popup').on('hide.bs.modal', function(){
        $('#general-checklist #accordion').find('.cat-to-be-deleted').removeClass('cat-to-be-deleted');
    });
    $('#general-checklist #accordion').on('click', '.btn-new-g-c-sub-cat', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $current = $(this).parent().siblings();
        var $target = $(this).parent().parent().parent().siblings('.sub-cat-list');
        var sub_cat = {
            'sub_category': $(this).parent().siblings().val(),
            'cat_id': $(this).parent().siblings().attr('data-catid'),
        };
        if($(this).parent().siblings().val()!==''){
            $.ajax({
                url: $siteUrl + 'general-checklist/save-new-sub-cat',
                type: 'post',
                dataType: 'json',
                data: sub_cat,
                success: function(data){
                    if(data.status === 'success'){
                        $target.prepend('<a href="#delete-sub-cat-popup" class="list-group-item sub-cat-item" data-toggle="modal" data-subcatid="'+data.sub_cat.checklist_id+
                                '">'+data.sub_cat.checklist_description+'<span class="glyphicon glyphicon-remove pull-right"></span></a>');
                        $current.val('');
                        $('.new-sub-category-form').find('.required-error').remove();
                        $('.alert-success').remove();
                        $('.btn-delete-event-types').before('<div class="alert alert-success">Event Type/s successfully deleted.</div>');
                        success_msg();
                    }else{
                        alert('error');
                    }
                },
                error: function(){
                    alert('error');
                }
            });
        }else{
            if(!$('.new-sub-category-form p').hasClass('required-error')){
                $('.new-sub-category-form').append('<p class="required-error">This field is required.</p>');
            }
        }
    });
    $('#general-checklist #accordion').on('click', '.sub-cat-item', function(){
        var $subCatId = $(this).attr('data-subcatid');
        $('#delete-sub-cat-popup .btn-yes').attr('data-subcatid', $subCatId);
        $(this).addClass('sub-cat-to-be-deleted');
    });
    $('#delete-sub-cat-popup .btn-yes').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $subCatId = $(this).attr('data-subcatid');
        $.ajax({
            url: $siteUrl + 'general-checklist/delete-sub-cat/' + $subCatId,
            type: 'post',
            success: function(){
                $('#general-checklist #accordion .sub-cat-list').find('.sub-cat-to-be-deleted').remove();
                $('#delete-sub-cat-popup').modal('hide');
            },
            error: function (){
               alert('error');
            }
        });
    });
    $('#delete-sub-cat-popup').on('hide.bs.modal', function(){
        $('#general-checklist #accordion .sub-cat-list').find('.sub-cat-to-be-deleted').removeClass('sub-cat-to-be-deleted');
    });
//Messages----------------------------------------------------------------------
    $('.btn-browse-file').click(function(){
        $('#compose-msg-popup #userfile').click();
    });
    $('#compose-msg-popup #userfile').change(function(){
       $('#compose-msg-popup #file-upload').val($(this).val()); 
    });
    //Inbox---------------------------------------------------------------------
//Contacts----------------------------------------------------------------------
    //Employees-----------------------------------------------------------------
    $('.btn-reset-emp-list').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        get_employees($siteUrl);
    });
    //new employee
    $('#new-emp-popup .another-address').click(function(){
        new_address_row($('#new-emp-popup').attr('id'));
    });
    $('#new-emp-popup .another-email').click(function(){
        new_email_row($('#new-emp-popup').attr('id'));
    });
    $('#new-emp-popup .another-contact-no').click(function(){
        new_contact_row($('#new-emp-popup').attr('id'));
    });
    $('#new-emp-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-emp-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-emp-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-emp-popup .btn-save').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#new-emp-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#new-emp-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#new-emp-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var emp = {
            'lname': $('#new-emp-popup .lname').val(),
            'fname': $('#new-emp-popup .fname').val(),
            'mname': $('#new-emp-popup .mname').val(),
            'bday': $('#new-emp-popup .bday').val(),
            'gender': $('#new-emp-popup .gender input[name=gender]:checked').val(),
            'date_emp': $('#new-emp-popup .date-emp').val(),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 0,
        };
        if(emp.lname!==''&&emp.fname!==''&&emp.mname!==''&&emp.bday!==''&&emp.date-emp!==''&&
                $('#new-emp-popup .address-1').val()!==''&&$('#new-emp-popup .email-1').val()!==''&&
                $('#new-emp-popup .contact-1').val()!==''){
            save_emp($siteUrl, emp, $('#new-emp-popup').attr('id'));
        }else{
            if(!$('#new-emp-popup p').hasClass('required-error')){
                $('#new-emp-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#new-emp-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    $('#new-emp-popup').on('hide.bs.modal', function(){
        $('#new-emp-popup .lname').val('');
        $('#new-emp-popup .fname').val('');
        $('#new-emp-popup .mname').val('');
        $('#new-emp-popup .bday').val('');
        $('#new-emp-popup .date-emp').val('');
        $('#new-emp-popup .address-1').val('');
        $('#new-emp-popup .email-1').val('');
        $('#new-emp-popup .contact-1').val('');
        $('#new-emp-popup .address-container').find('.d-address').parent().remove();
        $('#new-emp-popup .email-container').find('.d-email').parent().remove();
        $('#new-emp-popup .contact-no-container').find('.d-contact-no').parent().remove();
        $('#new-emp-popup').find('.required-error').remove();
    });
    // view/edit employee
    $('#view-emp-popup .another-address').click(function(){
        new_address_row($('#new-emp-popup').attr('id'));
    });
    $('#view-emp-popup .another-email').click(function(){
        new_email_row($('#new-emp-popup').attr('id'));
    });
    $('#view-emp-popup .another-contact-no').click(function(){
        new_contact_row($('#new-emp-popup').attr('id'));
    });
    $('#view-emp-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-emp-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-emp-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
        
    });
    $('#employees-tab .emp-list').on('click', '.btn-view-emp', function(){
        $(this).addClass('emp-viewed');
        $('#view-emp-popup #emp-id').val($(this).attr('data-empid'));
    });
    $('#view-emp-popup').on('show.bs.modal', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $empId = $('#view-emp-popup #emp-id').val();
        get_emp($siteUrl, $empId);
    });
    $('#view-emp-popup').on('hide.bs.modal', function(){
        $('#view-emp-popup .lname').val('');
        $('#view-emp-popup .fname').val('');
        $('#view-emp-popup .mname').val('');
        $('#view-emp-popup .bday').val('');
        $('#view-emp-popup .date-emp').val('');
        $('#view-emp-popup .address-container').find('.d-address').remove();
        $('#view-emp-popup .email-container').find('.d-email').remove();
        $('#view-emp-popup .contact-no-container').find('.d-contact-no').remove();
        $('#view-emp-popup').find('.required-error').remove();
        $('#employees-tab .emp-list').find('.emp-viewed').removeClass('emp-viewed');
    });
    $('#view-emp-popup .btn-update').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#view-emp-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#view-emp-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#view-emp-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var emp = {
            'emp_id': $('#view-emp-popup #emp-id').val(),
            'lname': $('#view-emp-popup .lname').val(),
            'fname': $('#view-emp-popup .fname').val(),
            'mname': $('#view-emp-popup .mname').val(),
            'bday': $('#view-emp-popup .bday').val(),
            'gender': $('#view-emp-popup .gender input[name=gender]:checked').val(),
            'date_emp': $('#view-emp-popup .date-emp').val(),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 1,
        };
        if(emp.lname!==''&&emp.fname!==''&&emp.mname!==''&&emp.bday!==''&&emp.date-emp!==''){
            save_emp($siteUrl, emp, $('#view-emp-popup').attr('id'));
        }else{
            if(!$('#view-emp-popup p').hasClass('required-error')){
                $('#view-emp-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#view-emp-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    // delete employees
    $('.emp-list .check-all-emp').click(function(){
        if($(this).is(':checked')) {
            if($('.emp-list tr td input[class=item-emp-checkbox]').hasClass('item-emp-checkbox')){
                $('.btn-delete-emp').removeClass('hide-element');
                $('.emp-list tr td input[class=item-emp-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-emp').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-emp').addClass('hide-element');
            $('.emp-list tr td input[class=item-emp-checkbox]').prop('checked', false);
        }
    });
    $('.emp-list').on('click', '.item-emp-checkbox', function(){
        var $checked_item = $('.emp-list .item-emp-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-emp').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-emp').removeClass('hide-element');
            }else{
                $('.btn-delete-emp').addClass('hide-element');
                $('.emp-list .check-all-emp').prop('checked', false);
            }
        }
    });
    $('#delete-emp-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var emp_id = new Array();
        $('.emp-list .item-emp-checkbox:checked').each(function(){
            emp_id.push($(this).val());
        });
        var type = {'emp_id':emp_id};
        $.ajax({
            url: $site_url + 'employee/delete',
            type: 'post',
            data: type,
            success: function (){
                $('#delete-emp-popup').modal('hide');
                $('.alert-success').remove();
                $('.btn-delete-emp').before('<div class="alert alert-success">Employee/s successfully deleted.</div>');
                success_msg();
                $('.emp-list .check-all-emp').prop('checked', false);
                $('.emp-list .item-emp-checkbox:checked').parent().parent().remove();
                $('.btn-delete-emp').addClass('hide-element');
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //Clients-------------------------------------------------------------------
    $('.client-nav a[data-toggle="tab"]').on('shown.bs.tab', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        $('.client-list tbody').empty();
        get_clients($siteUrl);
    });
    // new client
    $('#new-client-popup .another-address').click(function(){
        new_address_row($('#new-client-popup').attr('id'));
    });
    $('#new-client-popup .another-email').click(function(){
        new_email_row($('#new-client-popup').attr('id'));
    });
    $('#new-client-popup .another-contact-no').click(function(){
        new_contact_row($('#new-client-popup').attr('id'));
    });
    $('#new-client-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-client-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-client-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
        
    });
    $('#new-client-popup .btn-save').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#new-client-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#new-client-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#new-client-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var client = {
            'lname': $('#new-client-popup .lname').val(),
            'fname': $('#new-client-popup .fname').val(),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 0,
        };
        if(client.lname!==''&&client.fname!==''&&$('#new-client-popup .address-1').val()!==''&&
                $('#new-client-popup .email-1').val()!==''&&$('#new-client-popup .contact-1').val()!==''){
            save_client($siteUrl, client, $('#new-client-popup').attr('id'));
        }else{
            if(!$('#new-client-popup p').hasClass('required-error')){
                $('#new-client-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#new-client-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    $('#new-client-popup').on('hide.bs.modal', function(){
        $('#new-client-popup .lname').val('');
        $('#new-client-popup .fname').val('');
        $('#new-client-popup .address-1').val('');
        $('#new-client-popup .email-1').val('');
        $('#new-client-popup .contact-1').val('');
        $('#new-client-popup .address-container').find('.d-address').parent().remove();
        $('#new-client-popup .email-container').find('.d-email').parent().remove();
        $('#new-client-popup .contact-no-container').find('.d-contact-no').parent().remove();
        $('#new-client-popup').find('.required-error').remove();
    });
    // view/edit client
    $('#view-client-popup .another-address').click(function(){
        new_address_row($('#view-client-popup').attr('id'));
    });
    $('#view-client-popup .another-email').click(function(){
        new_email_row($('#view-client-popup').attr('id'));
    });
    $('#view-client-popup .another-contact-no').click(function(){
        new_contact_row($('#view-client-popup').attr('id'));
    });
    $('#view-client-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-client-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-client-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
        
    });
    $('.client-list').on('click', '.btn-view-client', function(){
        $(this).addClass('client-viewed');
        $('#view-client-popup #client-id').val($(this).attr('data-clientid'));
    });
    $('#view-client-popup').on('show.bs.modal', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $clientId = $('#view-client-popup #client-id').val();
        get_client($siteUrl, $clientId);
    });
    $('#view-client-popup').on('hide.bs.modal', function(){
        $('#view-client-popup .lname').val('');
        $('#view-client-popup .fname').val('');
        $('#view-client-popup .address-container').find('.d-address').remove();
        $('#view-client-popup .email-container').find('.d-email').remove();
        $('#view-client-popup .contact-no-container').find('.d-contact-no').remove();
        $('#view-client-popup').find('.required-error').remove();
        $('.client-list').find('.client-viewed').removeClass('client-viewed');
    });
    $('#view-client-popup .btn-update').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#view-client-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#view-client-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#view-client-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var client = {
            'client_id': $('#view-client-popup #client-id').val(),
            'lname': $('#view-client-popup .lname').val(),
            'fname': $('#view-client-popup .fname').val(),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 1,
        };
        if(client.lname!==''&&client.fname!==''){
            save_client($siteUrl, client, $('#view-client-popup').attr('id'));
        }else{
            if(!$('#view-client-popup p').hasClass('required-error')){
                $('#view-client-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#view-client-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    // delete clients
    $('.client-list .check-all-client').click(function(){
        if($(this).is(':checked')) {
            if($('.client-list tr td input[class=item-client-checkbox]').hasClass('item-client-checkbox')){
                $('.btn-delete-client').removeClass('hide-element');
                $('.client-list tr td input[class=item-client-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-client').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-client').addClass('hide-element');
            $('.client-list tr td input[class=item-client-checkbox]').prop('checked', false);
        }
    });
    $('.client-list').on('click', '.item-client-checkbox', function(){
        var $checked_item = $('.client-list .item-client-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-client').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-client').removeClass('hide-element');
            }else{
                $('.btn-delete-client').addClass('hide-element');
                $('.client-list .check-all-client').prop('checked', false);
            }
        }
    });
    $('#delete-client-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var client_id = new Array();
        $('.client-list .item-client-checkbox:checked').each(function(){
            client_id.push($(this).val());
        });
        var client = {'client_id':client_id};
        $.ajax({
            url: $site_url + 'client/delete',
            type: 'post',
            data: client,
            success: function (){
                $('#delete-client-popup').modal('hide');
                $('.alert-success').remove();
                $('.btn-delete-client').before('<div class="alert alert-success">Client/s successfully deleted.</div>');
                success_msg();
                $('.client-list .check-all-client').prop('checked', false);
                $('.client-list .item-client-checkbox:checked').parent().parent().remove();
                $('.btn-delete-client').addClass('hide-element');
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //Supplier-------------------------------------------------------------------
    $('.supplier-nav a[data-toggle="tab"]').on('shown.bs.tab', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        $('.supplier-list tbody').empty();
        get_all_supplier($siteUrl);
        
    });
    // new supplier
    $('#new-supplier-popup .another-services').click(function(){
        new_services_row($('#new-supplier-popup').attr('id'));
    });
    $('#new-supplier-popup .another-address').click(function(){
        new_address_row($('#new-supplier-popup').attr('id'));
    });
    $('#new-supplier-popup .another-email').click(function(){
        new_email_row($('#new-supplier-popup').attr('id'));
    });
    $('#new-supplier-popup .another-contact-no').click(function(){
        new_contact_row($('#new-supplier-popup').attr('id'));
    });
    $('#new-supplier-popup').on('click', '.btn-remove-services', function(){
        $(this).parent().parent().remove();
    });
    $('#new-supplier-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
    });
    $('#new-supplier-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
    });
    $('#new-supplier-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
    });
    $('#new-supplier-popup .btn-save').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var services = new Array();
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#new-supplier-popup .services-container .services-text').each(function(){
            services.push($(this).val());
        });
        $('#new-supplier-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#new-supplier-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#new-supplier-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var supplier = {
            'cname': $('#new-supplier-popup .cname').val(),
            'contact_person': $('#new-supplier-popup .contact-person').val(),
            'services': services.join(';'),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 0,
        };
        if(supplier.cname!==''&&supplier.contact_person!==''&&$('#new-supplier-popup .services-1').val()!==''&&
                $('#new-supplier-popup .address-1').val()!==''&&$('#new-supplier-popup .email-1').val()!==''&&
                $('#new-supplier-popup .contact-1').val()!==''){
            save_supplier($siteUrl, supplier, $('#new-supplier-popup').attr('id'));
        }else{
            if(!$('#new-supplier-popup p').hasClass('required-error')){
                $('#new-supplier-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#new-supplier-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    $('#new-supplier-popup').on('hide.bs.modal', function(){
        $('#new-supplier-popup .cname').val('');
        $('#new-supplier-popup .services-1').val('');
        $('#new-supplier-popup .address-1').val('');
        $('#new-supplier-popup .email-1').val('');
        $('#new-supplier-popup .contact-1').val('');
        $('#new-supplier-popup .services-container').find('.d-address').parent().remove();
        $('#new-supplier-popup .address-container').find('.d-address').parent().remove();
        $('#new-supplier-popup .email-container').find('.d-email').parent().remove();
        $('#new-supplier-popup .contact-no-container').find('.d-contact-no').parent().remove();
        $('#new-supplier-popup').find('.required-error').remove();
    });
    // view/edit supplier
    $('#view-supplier-popup .another-services').click(function(){
        new_services_row($('#view-supplier-popup').attr('id'));
    });
    $('#view-supplier-popup .another-address').click(function(){
        new_address_row($('#view-supplier-popup').attr('id'));
    });
    $('#view-supplier-popup .another-email').click(function(){
        new_email_row($('#view-supplier-popup').attr('id'));
    });
    $('#view-supplier-popup .another-contact-no').click(function(){
        new_contact_row($('#view-supplier-popup').attr('id'));
    });
    $('#view-supplier-popup').on('click', '.btn-remove-services', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-supplier-popup').on('click', '.btn-remove-address', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-supplier-popup').on('click', '.btn-remove-email', function(){
        $(this).parent().parent().remove();
        
    });
    $('#view-supplier-popup').on('click', '.btn-remove-contact-no', function(){
        $(this).parent().parent().remove();
        
    });
    $('.supplier-list').on('click', '.btn-view-supplier', function(){
        $(this).addClass('supplier-viewed');
        $('#view-supplier-popup #supplier-id').val($(this).attr('data-supplierid'));
    });
    $('#view-supplier-popup').on('show.bs.modal', function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var $supplierId = $('#view-supplier-popup #supplier-id').val();
        get_supplier($siteUrl, $supplierId);
    });
    $('#view-supplier-popup').on('hide.bs.modal', function(){
        $('#view-supplier-popup .cname').val('');
        $('#view-supplier-popup .contact-person').val('');
        $('#view-supplier-popup .services-container').find('.d-services').remove();
        $('#view-supplier-popup .address-container').find('.d-address').remove();
        $('#view-supplier-popup .email-container').find('.d-email').remove();
        $('#view-supplier-popup .contact-no-container').find('.d-contact-no').remove();
        $('#view-supplier-popup').find('.required-error').remove();
        $('.supplier-list').find('.supplier-viewed').removeClass('supplier-viewed');
    });
    $('#view-supplier-popup .btn-update').click(function(){
        var $siteUrl = $('body').attr('data-siteurl');
        var services = new Array();
        var address = new Array();
        var email = new Array();
        var contact = new Array();
        $('#view-supplier-popup .services-container .services-text').each(function(){
            services.push($(this).val());
        });
        $('#view-supplier-popup .address-container .address-text').each(function(){
            address.push($(this).val());
        });
        $('#view-supplier-popup .email-container .email-text').each(function(){
            email.push($(this).val());
        });
        $('#view-supplier-popup .contact-no-container .contact-no-text').each(function(){
            contact.push($(this).val());
        });
        var supplier = {
            'supplier_id': $('#view-supplier-popup #supplier-id').val(),
            'cname': $('#view-supplier-popup .cname').val(),
            'contact_person': $('#view-supplier-popup .contact-person').val(),
            'services': address.join(';'),
            'address': address.join(';'),
            'email': email.join(';'),
            'contact_no': contact.join(';'),
            'action': 1,
        };
        if(supplier.cname!==''&&supplier.contact_person!==''){
            save_supplier($siteUrl, supplier, $('#view-supplier-popup').attr('id'));
        }else{
            if(!$('#view-supplier-popup p').hasClass('required-error')){
                $('#view-supplier-popup .required').parent().append('<p class="required-error">This field is required.</p>');
                $('#view-supplier-popup .required-info').parent().after('<p class="required-error">This field is required.</p>');
            }
        }
    });
    // delete supplier
    $('.supplier-list .check-all-supplier').click(function(){
        if($(this).is(':checked')) {
            if($('.supplier-list tr td input[class=item-supplier-checkbox]').hasClass('item-supplier-checkbox')){
                $('.btn-delete-supplier').removeClass('hide-element');
                $('.supplier-list tr td input[class=item-supplier-checkbox]').prop('checked', true);
            }else{
                $('.btn-delete-supplier').addClass('hide-element');
                $(this).prop('checked', false);
            }
        }else{
            $('.btn-delete-supplier').addClass('hide-element');
            $('.supplier-list tr td input[class=item-supplier-checkbox]').prop('checked', false);
        }
    });
    $('.supplier-list').on('click', '.item-supplier-checkbox', function(){
        var $checked_item = $('.client-list .item-supplier-checkbox:checked').size();
        if($(this).is(':checked')){
            $('.btn-delete-supplier').removeClass('hide-element');
        }else{
            if($checked_item>0){
                $('.btn-delete-supplier').removeClass('hide-element');
            }else{
                $('.btn-delete-supplier').addClass('hide-element');
                $('.supplier-list .check-all-supplier').prop('checked', false);
            }
        }
    });
    $('#delete-supplier-popup .btn-yes').click(function(){
        var $site_url = $('body').attr('data-siteurl');
        var supplier_id = new Array();
        $('.supplier-list .item-supplier-checkbox:checked').each(function(){
            supplier_id.push($(this).val());
        });
        var supplier = {'supplier_id':supplier_id};
        $.ajax({
            url: $site_url + 'supplier/delete',
            type: 'post',
            data: supplier,
            success: function (){
                $('#delete-supplier-popup').modal('hide');
                $('.alert-success').remove();
                $('.btn-delete-supplier').before('<div class="alert alert-success">Supplier successfully deleted.</div>');
                success_msg();
                $('.supplier-list .check-all-supplier').prop('checked', false);
                $('.supplier-list .item-supplier-checkbox:checked').parent().parent().remove();
                $('.btn-delete-supplier').addClass('hide-element');
            },
             error: function(){
                 alert('error');
             }
         });
    });
    //Functions-----------------------------------------------------------------
    // get suppliers
    function get_all_supplier(siteUrl){
        $.ajax({
            url: siteUrl + 'supplier/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                if(data!==false){
                    $.each(data, function(key, value){
                        $('.supplier-list tbody').append('<tr>'+
                                '<td><input type="checkbox" class="item-supplier-checkbox" value="'+
                                value.supplier_id+'" /></td>'+
                                '<td><a href="#view-supplier-popup" class="btn-view-supplier text-supplier-company" data-toggle="modal" data-supplierid="'+
                                value.supplier_id+'">'+value.company+'</a></td>'+
                                '<td class="text-supplier-contact-person">'+value.contact_person+'</td>'+
                                '<td class="text-supplier-services">'+value.services+'</td>'+
                                '<td class="text-supplier-address">'+value.s_address+'</td>'+
                                '<td class="text-supplier-email">'+value.s_email+'</td>'+
                                '<td class="text-supplier-contact">'+value.s_num+'</td>'+
                                '</tr>');
                    });
                }else{
                    if(!$('#supplier-tab div').hasClass('empty-content')){
                        $('.supplier-list').after('<div class="alert alert-info empty-content">Oops! You currently dont have any supplier.</div>');
                    }
                }
            },
            error: function(){
                alert('error'); 
            }
        });
    }
    // get supplier
    function get_supplier(siteUrl, supplierId){
        $.ajax({
            url: siteUrl + 'supplier/view/' + supplierId,
            type: 'post',
            dataType: 'json',
            success: function(data) {
                $('#view-supplier-popup #supplier-id').val(data.supplier_id);
                $('#view-supplier-popup .cname').val(data.company);
                $('#view-supplier-popup .contact-person').val(data.contact_person);
                var services = data.services.split(';');
                var address = data.s_address.split(';');
                var email = data.s_email.split(';');
                var contact = data.s_num.split(';');
                for(var i=0;i<services.length;i++){
                    $('#view-supplier-popup .another-services').after('<input type="text" value="'+services[i]
                            +'" class="form-control services-text d-services required-info" name="address">');
                }
                for(var i=0;i<address.length;i++){
                    $('#view-supplier-popup .another-address').after('<input type="text" value="'+address[i]
                            +'" class="form-control address-text d-address required-info" name="address">');
                }
                for(var i=0;i<email.length;i++){
                    $('#view-supplier-popup .another-email').after('<input type="text" value="'+email[i]
                            +'" class="form-control email-text d-email required-info" name="address">');
                }
                for(var i=0;i<contact.length;i++){
                    $('#view-supplier-popup .another-contact-no').after('<input type="text" value="'+contact[i]
                            +'" class="form-control contact-no-text d-contact-no required-info" name="address">');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    // save supplier
    function save_supplier(siteUrl, supplier, trigger){
        var item = trigger.split('-');
        $.ajax({
            url: siteUrl + 'supplier/save',
            type: 'post',
            data: supplier,
            dataType: 'json',
            success: function(data){
                if(data.status==='success'){
                    if(item[0]==='new'){
                        $('#supplier-tab .supplier-list tbody')
                            .prepend('<tr>'+
                            '<td><input type="checkbox" class="item-supplier-checkbox" value="'+
                            data.supplier.supplier_id+'" /></td>'+
                            '<td><a href="#view-supplier-popup" class="btn-view-supplier text-supplier-company" data-toggle="modal" data-supplierid="'+
                            data.supplier.supplier_id+'">'+data.supplier.company+'</a></td>'+
                            '<td class="text-supplier-contact-person">'+data.supplier.contact_person+'</td>'+
                            '<td class="text-supplier-services">'+data.supplier.services+'</td>'+
                            '<td class="text-supplier-address">'+data.supplier.s_address+'</td>'+
                            '<td class="text-supplier-email">'+data.supplier.s_email+'</td>'+
                            '<td class="text-supplier-contact">'+data.supplier.s_num+'</td>'+
                            '</tr>');
                        $('#supplier-tab').find('.empty-content').remove();
                        $('.alert-success').remove();
                        $('.btn-delete-supplier').before('<div class="alert alert-success">Supplier <b>'+supplier.cname+'</b> saved.</div>');
                        success_msg();
                    }else if(item[0]==='view'){
                        $('.alert-success').remove();
                        $('.btn-delete-supplier').before('<div class="alert alert-success">Supplier <b>'+data.supplier.company+'</b> successfully updated.</div>');
                        success_msg();
                        $('.supplier-viewed').text(data.supplier.company);
                        $('.supplier-viewed').parent().siblings('.text-supplier-contact-person').text(data.supplier.contact_person);
                        $('.supplier-viewed').parent().siblings('.text-supplier-services').text(data.supplier.services);
                        $('.supplier-viewed').parent().siblings('.text-supplier-address').text(data.supplier.s_address);
                        $('.supplier-viewed').parent().siblings('.text-supplier-email').text(data.supplier.s_email);
                        $('.supplier-viewed').parent().siblings('.text-supplier-contact').text(data.supplier.s_num);
                    }
                    $('#'+trigger).modal('hide');
                }else{
                    alert('errors');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    function get_clients(siteUrl){
        $.ajax({
            url: siteUrl + 'clients/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                if(data!==false){
                    $.each(data, function(key, value){
                        $('.client-list tbody').append('<tr>'+
                                '<td><input type="checkbox" class="item-client-checkbox" value="'+
                                value.client_id+'" /></td>'+
                                '<td><a href="#view-client-popup" class="btn-view-client text-client-name" data-toggle="modal" data-clientid="'+
                                value.client_id+'">'+value.client_lname+', '+value.client_fname+'</a></td>'+
                                '<td class="text-client-address">'+value.client_address+'</td>'+
                                '<td class="text-client-email">'+value.client_email+'</td>'+
                                '<td class="text-client-contact">'+value.client_num+'</td>'+
                                '</tr>');
                    });
                }else{
                    if(!$('#clients-tab div').hasClass('empty-content')){
                        $('.client-list').after('<div class="alert alert-info empty-content">Oops! You currently dont have any client.</div>');
                    }
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    // get client
    function get_client(siteUrl, clientId){
        $.ajax({
            url: siteUrl + 'client/view/' + clientId,
            type: 'post',
            dataType: 'json',
            success: function(data) {
                $('#view-client-popup #client-id').val(data.client_id);
                $('#view-client-popup .lname').val(data.client_lname);
                $('#view-client-popup .fname').val(data.client_fname);
                var address = data.client_address.split(';');
                var email = data.client_email.split(';');
                var contact = data.client_num.split(';');
                for(var i=0;i<address.length;i++){
                    $('#view-client-popup .another-address').after('<input type="text" value="'+address[i]
                            +'" class="form-control address-text d-address required-info" name="address">');
                }
                for(var i=0;i<email.length;i++){
                    $('#view-client-popup .another-email').after('<input type="text" value="'+email[i]
                            +'" class="form-control email-text d-email required-info" name="address">');
                }
                for(var i=0;i<contact.length;i++){
                    $('#view-client-popup .another-contact-no').after('<input type="text" value="'+contact[i]
                            +'" class="form-control contact-no-text d-contact-no required-info" name="address">');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    // save client
    function save_client(siteUrl, client, trigger){
        var item = trigger.split('-');
        $.ajax({
            url: siteUrl + 'client/save',
            type: 'post',
            data: client,
            dataType: 'json',
            success: function(data){
                if(data.status==='success'){
                    if(item[0]==='new'){
                        $('#clients-tab .client-list tbody')
                            .prepend('<tr>'+
                            '<td><input type="checkbox" class="item-client-checkbox" value="'+
                            data.client.client_id+'" /></td>'+
                            '<td><a href="#view-client-popup" class="btn-view-client text-client-name" data-toggle="modal" data-clientid="'+
                            data.client.client_id+'">'+data.client.client_lname+', '+data.client.client_fname+'</a></td>'+
                            '<td class="text-client-address">'+data.client.client_address+'</td>'+
                            '<td class="text-client-email">'+data.client.client_email+'</td>'+
                            '<td class="text-client-contact">'+data.client.client_num+'</td>'+
                            '</tr>');
                        $('#clients-tab').find('.empty-content').remove();
                        $('.alert-success').remove();
                        $('.btn-delete-client').before('<div class="alert alert-success">Client <b>'+client.lname+', '+client.fname+'</b> saved.</div>');
                        success_msg();
                    }else if(item[0]==='view'){
                        $('.alert-success').remove();
                        $('.btn-delete-client').before('<div class="alert alert-success">Client <b>'+data.client.client_lname+', '+data.client.client_fname+'</b> successfully updated.</div>');
                        $('.client-viewed').text(data.client.client_lname+', '+data.client.client_fname);
                        $('.client-viewed').parent().siblings('.text-client-address').text(data.client.client_address);
                        $('.client-viewed').parent().siblings('.text-client-email').text(data.client.client_email);
                        $('.client-viewed').parent().siblings('.text-client-contact').text(data.client.client_num);
                    }
                    $('#'+trigger).modal('hide');
                }else{
                    alert('errors');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    // get employees
    function get_employees(siteUrl){
        $.ajax({
            url: siteUrl + 'employees/get',
            type: 'post',
            dataType: 'json',
            success: function(data){
                $('.emp-list tbody').empty();
                $.each(data, function(key, value){
                    var g = '';
                    if(value.gender=='0'){
                        g = 'Male';
                    }else{
                        g = 'Female';
                    }
                    $('.emp-list tbody')
                            .append('<tr><td><input type="checkbox" class="item-emp-checkbox" value="'+
                            value.coordinator_id+'"/></td><td><a href="#view-emp-popup" class="btn-view-emp text-emp-name"'+ 
                            'data-toggle="modal" data-empid="'+value.coordinator_id+'">'+value.lname+', '+value.fname+'</a></td>'+
                            '<td class="text-emp-gender">'+g+'</td><td class="text-emp-address">'+value.coord_address+'</td>'+
                            '<td class="text-emp-email">'+value.coord_email+'</td><td class="text-emp-contact">'+value.coord_num+'</td>'+
                            '<td class="text-emp-date">'+value.emp_date+'</td></tr>');
                });
            },
            error: function(){
                $('.btn-delete-emp').before('<div class="alert alert-danger">This is embarrassing! We are not able to find the data.</div>');
                error_msg();
            }
        });
    }
    // get employee
    function get_emp(siteUrl, empId){
        $.ajax({
            url: siteUrl + 'employee/view/' + empId,
            type: 'post',
            dataType: 'json',
            success: function(data) {
                $('#view-emp-popup #emp-id').val(data.coordinator_id);
                $('#view-emp-popup .lname').val(data.lname);
                $('#view-emp-popup .fname').val(data.fname);
                $('#view-emp-popup .mname').val(data.mname);
                $('#view-emp-popup .bday').val(data.bday);
                $('#view-emp-popup .emp-date').val(data.emp_date);
                if(data.gender == '0') {
                    $('#view-emp-popup .male').attr('checked', true).parent().addClass('active');
                    $('#view-emp-popup .female').attr('checked', false).parent().removeClass('active');
                }else{
                    $('#view-emp-popup .female').attr('checked', true).parent().addClass('active');
                    $('#view-emp-popup .male').attr('checked', false).parent().removeClass('active');
                }
                var address = data.coord_address.split(';');
                var email = data.coord_email.split(';');
                var contact = data.coord_num.split(';');
                for(var i=0;i<address.length;i++){
                    $('#view-emp-popup .another-address').after('<input type="text" value="'+address[i]
                            +'" class="form-control address-text d-address required-info" name="address">');
                }
                for(var i=0;i<email.length;i++){
                    $('#view-emp-popup .another-email').after('<input type="text" value="'+email[i]
                            +'" class="form-control email-text d-email required-info" name="address">');
                }
                for(var i=0;i<contact.length;i++){
                    $('#view-emp-popup .another-contact-no').after('<input type="text" value="'+contact[i]
                            +'" class="form-control contact-no-text d-contact-no required-info" name="address">');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    // save employee
    function save_emp(siteUrl, emp, trigger){
        var item = trigger.split('-');
        $.ajax({
            url: siteUrl + 'employee/save',
            type: 'post',
            data: emp,
            dataType: 'json',
            success: function(data){
                var g = '';
                if(data.emp.gender=='0'){
                    g = 'Male';
                }else if(data.emp.gender=='1'){
                    g = 'Female';
                }
                if(data.status==='success'){
                    if(item[0]==='new'){
                        $('#employees-tab .emp-list tbody')
                            .prepend('<tr>'+
                            '<td><input type="checkbox" class="item-emp-checkbox" value="'+data.emp.coordinator_id+'"/></td>'+
                            '<td><a href="#view-emp-popup" class="btn-view-emp text-emp-name" data-toggle="modal" data-empid="'+data.emp.coordinator_id+'">'+
                            data.emp.lname+', '+data.emp.fname+'</a></td><td class="text-emp-gender">'+g+'</td><td class="text-emp-address">'+data.emp.coord_address+'</td>'+
                            '<td class="text-emp-email">'+data.emp.coord_email+'</td><td class="text-emp-contact>'+data.emp.coord_num+'</td><td class="text-emp-date">'
                            +data.emp.emp_date+'</td></tr>');
                        $('#employees-tab').find('.empty-content').remove();
                        $('.alert-success').remove();
                        $('.btn-delete-emp').before('<div class="alert alert-success">Employee <b>'+emp.lname+', '+emp.fname+'</b> saved.</div>');
                        success_msg();
                    }else if(item[0]==='view'){
                        $('.alert-success').remove();
                        $('.btn-delete-emp').before('<div class="alert alert-success">Employee <b>'+data.emp.lname+', '+data.emp.fname+'</b> successfully updated.</div>');
                        success_msg();
                        $('.emp-viewed').text(data.emp.lname+', '+data.emp.fname);
                        $('.emp-viewed').parent().siblings('.text-emp-gender').text(g);
                        $('.emp-viewed').parent().siblings('.text-emp-address').text(data.emp.coord_address);
                        $('.emp-viewed').parent().siblings('.text-emp-email').text(data.emp.coord_email);
                        $('.emp-viewed').parent().siblings('.text-emp-contact').text(data.emp.coord_num);
                        $('.emp-viewed').parent().siblings('.text-emp-date').text(data.emp.emp_date);
                    }
                    $('#'+trigger).modal('hide');
                }else{
                    alert('errors');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    function new_services_row(trigger){
        $('#'+trigger+' .services-container').
                append('<div class="input-group">'+
                '<input type="text" class="form-control services-text d-services" name="address">'+
                '<span class="input-group-btn">'+
                '<button class="btn btn-default btn-remove-services" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
                '</span></div>');
    }
    
    function new_address_row(trigger){
        $('#'+trigger+' .address-container').
                append('<div class="input-group">'+
                '<input type="text" class="form-control address-text d-address" name="address">'+
                '<span class="input-group-btn">'+
                '<button class="btn btn-default btn-remove-address" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
                '</span></div>');
    }
    function new_email_row(trigger){
        $('#'+trigger+' .email-container').
                append('<div class="input-group">'+
                '<input type="text" class="form-control email-text d-email" name="email">'+
                '<span class="input-group-btn">'+
                '<button class="btn btn-default btn-remove-email" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
                '</span></div>');
    };
    function new_contact_row(trigger){
        $('#'+trigger+' .contact-no-container').
                append('<div class="input-group">'+
                '<input type="text" class="form-control contact-no-text d-contact-no" name="email">'+
                '<span class="input-group-btn">'+
                '<button class="btn btn-default btn-remove-contact-no" type="button"><span class="glyphicon glyphicon-remove"></span></button>'+
                '</span></div>');
    };
    // get venue
    function get_venue(site_url, venue_id){
        $.ajax({
            url: site_url + 'venue/edit/'+venue_id,
            type: 'post',
            dataType: 'json',
            success: function(data){
                $('#edit-venue-popup #e-venue-name').val(data.venue_name);
                $('#edit-venue-popup #e-venue-address').val(data.venue_address);
            },
            error: function(){
        
            }
        });
    }
    //save venue
    function save_venue(site_url, venue, trigger){
        var item = trigger.split('-');
        $.ajax({
            url: site_url + 'venue/save',
            type: 'post',
            dataType: 'json',
            data: venue,
            success: function(data) {
                if(data.status !== 'error'){
                    $('#'+trigger).modal('hide');
                    if(item[0]==='new'){
                        $('.alert-success').remove();
                        $('.btn-delete-venues').before('<div class="alert alert-success">Venue <b>'+data.venue.venue_name+'</b> successfully saved.</div>');
                        success_msg();
                        $('.venues-list tbody').prepend('<tr>'+
                                '<td><input type="checkbox" class="item-venue-checkbox" value="'+
                                data.venue.venue_id+'" /></td>'+
                                '<td class="text-venue-name">'+data.venue.venue_name+'</td>'+
                                '<td class="text-venue-address">'+data.venue.venue_address+'</td>'+
                                '<td><button class="btn btn-primary btn-edit-venues btn-xs" data-toggle="modal"'+
                                'data-target="#edit-venue-popup" data-venueid="'+data.venue.venue_id+'" title="Edit Venue"><span class="glyphicon glyphicon-edit"></span>'+
                                '</button></td>'+
                                '</tr>');
                    }else if(item[0]==='edit'){
                        $('.alert-success').remove();
                        $('.btn-delete-venues').before('<div class="alert alert-success">Venue <b>'+data.venue.venue_name+'</b> successfully updated.</div>');
                        success_msg();
                        $('.updated-venue').parent().siblings('.text-venue-name').text(data.venue.venue_name);
                        $('.updated-venue').parent().siblings('.text-venue-address').text(data.venue.venue_address);
                    }
                }else{
                    $('#'+trigger+' .modal-body').prepend('<div class="alert alert-danger">'+data.msg+'</div>');
                }
            },
            error: function(){
                alert('error');
            }
        });
    }
    //remove success message after 5sec
    function success_msg() {
        setTimeout(function(){
            $('.alert-success').fadeOut('slow', function(){$(this).remove();});
        },5000);
    }
    function error_msg() {
        setTimeout(function(){
            $('.alert-danger').fadeOut('slow', function(){$(this).remove();});
        },5000);
    }
    //Event Organizer
	$("#create-event").on("click",function() {
		$("#event-detail-menu").show();
		$("#event-details").show();
		$("#event-supplier").hide();
		$("#event-checklist").hide();
		$("#coord-task").hide();
		$("#list-view").hide();
		$("#cal-view").hide();	
	});

	$("#cal-view-btn").on("click",function() {
		$("#event-detail-menu").hide();
		$("#event-details").hide();
		$("#event-supplier").hide();
		$("#event-checklist").hide();
		$("#coord-task").hide();
		$("#list-view").hide();
		$("#cal-view").show();	
	});

	$("#list-view-btn").on("click",function() {
		$("#event-detail-menu").hide();
		$("#event-details").hide();
		$("#event-supplier").hide();
		$("#event-checklist").hide();
		$("#coord-task").hide();
		$("#list-view").show();
		$("#cal-view").hide();	
	});

	$("#event").on("click",function() {
		$("#event-detail-menu").show();		
		$("#event-details").show();
		$("#event-supplier").hide();
		$("#event-checklist").hide();
		$("#coord-task").hide();
		$("#list-view").hide();
		$("#cal-view").hide();
	});

	$("#supplier").on("click",function() {
		$("#eventD-detail-menu").show();
		$("#event-details").hide();
		$("#event-supplier").show();
		$("#event-checklist").hide();
		$("#coord-task").hide();
		$("#list-view").hide();
		$("#calv-view").hide();
	});
	$("#checklist").on("click",function() {
		$("#event-detail-menu").show();
		$("#event-details").hide();
		$("#event-supplier").hide();
		$("#event-checklist").show();
		$("#coord-task").hide();
		$("#list-view").hide();
		$("#cal-view").hide();
	});
	$("#task").on("click",function() {
		$("#event-detail-menu").show();
		$("#event-details").hide();
		$("#event-supplier").hide();
		$("#event-checklist").hide();
		$("#coord-task").show();
		$("#list-view").hide();
		$("#cal-view").hide();
	});
    $('#files-table').dataTable({
        "aoColumnDefs":[{
                "bSortable": false, 
                "aTargets":[0,4]}]
    });
});